####-------------------------------------------------------------------------------------####
# File to recode constituent cross sections.
####-------------------------------------------------------------------------------------####
rm(list=ls())
require(foreign)
require(car)
####-------------------------------------------------------------------------------------####
###  File Description ##

# Internet Recontact Survey #
rm(list=ls())
require(foreign)
require(car)
require(psych)
# Start by merging 2012 datasets
anes.2012a<-read.dta("/Users/chrisweber/Dropbox/Project Folder/projects/Stanley_Authoritarianism/data/internet recontact/anes_panel_2013_inetrecontact.dta",
                    convert.factors=FALSE)
#anes.2012b<-read.dta("/Users/chrisweber/Dropbox/Project Folder/projects/Stanley_Authoritarianism/data/anes_timeseries_2012_dta/anes_timeseries_2012_Stata12.dta",
#                    convert.factors=FALSE)
#anes.2012<-merge(anes.2012a, anes.2012b, by="caseid")
load("/Users/chrisweber/Dropbox/Project Folder/projects/Stanley_Authoritarianism/data/auth.data.Rdata")
data<-subset(auth.data, year==2012)
data$caseid<-data$rid
anes.2012<-merge(anes.2012a, data, by=c("caseid"), all.x=T)

## Right Wing Authoritarianism ## 
rwa1<-recode(anes.2012$C5_U1, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA")  #Everyone has to create their own way*
rwa2<-recode(anes.2012$C5_U2, "1=1; 2=2; 3=3; 4=4; 5=5; else=NA")  #Our country needs free thinkers*
rwa3<-recode(anes.2012$C5_U3, "1=5; 2=4; 3=3; 4=1; 5=1; else=NA")  #Place faith in authority figures
rwa4<-recode(anes.2012$C5_U4, "1=5; 2=4; 3=3; 4=1; 5=1; else=NA")  #Needs strong leaders
rwa5<-recode(anes.2012$C5_U5, "1=5; 2=4; 3=3; 4=1; 5=1; else=NA")  #Old fashioned ways
rwa<-cbind(rwa1, rwa2, rwa3, rwa4, rwa5)
rwa<-recode(rwa, "1:2=1; 3=2; 4:5=3")
alpha(data.frame(rwa))
rwa.score<-rowMeans(cbind(rwa1, rwa2, rwa3, rwa4, rwa5), na.rm=T)

## Child rearing
#
child1<-recode(anes.2012$C5_H1, "1=0; 2=1; else=NA")  #Respect for elders
child2<-recode(anes.2012$C5_H2, "1=1; 2=0; else=NA")  #obedience
child3<-recode(anes.2012$C5_H3, "1=0; 2=1; else=NA")  #good manners
child4<-recode(anes.2012$C5_H4, "1=0; 2=1; else=NA")  #considerate
alpha(data.frame(cbind(child1, child2, child3, child4)))
child.score<-rowMeans(cbind(child1, child2, child3, child4), na.rm=T)
child<-cbind(child1, child2, child3, child4)

##Correlation of 0.45
alpha(data.frame(rwa[anes.2012$black==1,]))
alpha(data.frame(rwa[anes.2012$white==1,]))  ## Big racial difference in measurement here
alpha(data.frame(child[anes.2012$black==1,]))
alpha(data.frame(child[anes.2012$white==1,])) ##Not large difference here.


#
### Recode the ANES items ## 
### Replicate the larger Model ###

##Race

# Uncomment if use 2012, I'm using the cumulative data.
#black<-anes.2012$dem_racecps_black  ## At least one mention of black
#white<-anes.2012$dem_racecps_white  ## At least one mention of white
#black.v.white<-NA
#black.v.white[black==1 & white==0]<-1
#black.v.white[black==0 & white==1]<-0
#alpha(data.frame(rwa[black.v.white==0,]))
#alpha(data.frame(rwa[black.v.white==1,]))


#### Results #### 

alpha(data.frame(rwa[anes.2012$black==1,]))
alpha(data.frame(rwa[anes.2012$white==1,]))  ## Big racial difference in measurement here
alpha(data.frame(child[anes.2012$black==1,]))
alpha(data.frame(child[anes.2012$white==1,])) ##Not large difference here.

### Explore Racial Differences ###
summary(lm(child.score~black+hispanic+other,
           data=anes.2012)
)
summary(lm(rwa.score~black+black+hispanic+other,
           data=anes.2012)
)
summary(lm(child.score~female+college+
             south+northeast+west+income+
             +ideology+pid+
             black+hispanic+other,
           data=anes.2012)
)
summary(lm(rwa.score~female+college+
             south+northeast+west+income+
             +ideology+pid+
             black+hispanic+other,
           data=anes.2012)
)
## With Church Covariates ##
summary(lm(child.score~church+
             catholic+
             jewish+bible+female+college+
             south+northeast+west+income+
             +ideology+pid+
             black+hispanic+other,
           data=anes.2012)
)
summary(lm(rwa.score~church+
             catholic+
             jewish+bible+female+college+
             south+northeast+west+income+
             +ideology+pid+
             black+hispanic+other,
           data=anes.2012)
)  ## The effect is somewhat reduced for blacks, when controlling for church attendance.

### Replicate the Issue Models with RWA ##

############################################
############################################
############################################

##Ideology###
summary(polr(as.factor(ideology*6+1)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))


##Feelings towards Republicans/Democrats
summary(lm(feeling.rep~female+college+
             south+northeast+west+income+church+
             other+catholic+
             jewish+bible+church+ideology+pid+
             rwa.score+
             black+hispanic+other+black:rwa.score,
           anes.2012))

##Feelings towards Republicans/Democrats
summary(lm(feeling.dem~female+college+
             south+northeast+west+income+church+
             other+catholic+
             jewish+bible+church+ideology+pid+
             rwa.score+
             black+hispanic+other+black:rwa.score,
           anes.2012))


## Abortion##
summary(polr(as.factor(abortion)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))
summary(polr(as.factor(domestic.spending)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))
summary(polr(as.factor(gov.living)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))
summary(polr(as.factor(pid)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))
summary(polr(as.factor(aid.blacks)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))

summary(polr(as.factor(defense.spending)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012))
summary(glm(vote~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               rwa.score+
               black+hispanic+other+black:rwa.score,
             anes.2012, family=binomial("logit")))



### Replicate the Issue Models with child rearing ##

############################################
############################################
############################################

##Ideology###
summary(polr(as.factor(ideology*6+1)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))


##Feelings towards Republicans/Democrats
summary(lm(feeling.rep~female+college+
             south+northeast+west+income+church+
             other+catholic+
             jewish+bible+church+ideology+pid+
             child.score+
             black+hispanic+other+black:child.score,
           anes.2012))

##Feelings towards Republicans/Democrats
summary(lm(feeling.dem~female+college+
             south+northeast+west+income+church+
             other+catholic+
             jewish+bible+church+ideology+pid+
             child.score+
             black+hispanic+other+black:child.score,
           anes.2012))


## Abortion##
summary(polr(as.factor(abortion)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))
summary(polr(as.factor(domestic.spending)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))
summary(polr(as.factor(gov.living)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))
summary(polr(as.factor(pid)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))
summary(polr(as.factor(aid.blacks)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))

summary(polr(as.factor(defense.spending)~female+college+
               south+northeast+west+income+church+
               other+catholic+
               jewish+bible+church+ideology+pid+
               child.score+
               black+hispanic+other+black:child.score,
             anes.2012))
summary(glm(vote~female+college+
              south+northeast+west+income+church+
              other+catholic+
              jewish+bible+church+ideology+pid+
              child.score+
              black+hispanic+other+black:child.score,
            anes.2012, family=binomial("logit")))




# data<-data.frame(cbind(child1, child2, child3, child4, rwa, black.v.white))
# table(data$rwa1[black==1])
# table(data$rwa2[black==1])
# table(data$rwa3[black==1])
# table(data$rwa4[black==1])
# table(data$rwa5[black==1])
# 
# data<-subset(data, !is.na(data$black.v.white))
# write.csv(data,
#           file="/Users/chrisweber/Dropbox/Project Folder/projects/Stanley_Authoritarianism/data/mplus.csv")